package com.javaweb.admin.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javaweb.admin.entity.C37InfCkScBillNew;
import com.javaweb.admin.entity.KDLMaterial;
import com.javaweb.admin.entity.NcTaskLog;
import com.javaweb.admin.entity.Scheduled;
import com.javaweb.admin.mapper.ScheduledMapper;
import com.javaweb.admin.service.AbstractJob;
import com.javaweb.admin.service.INcTaskLogService;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import com.seegw.shaphar.IKDLService;
import com.sphchina.esb.webservice.ISendToReviewLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UploadKdlMaterial extends AbstractJob {
    @Autowired
    INcTaskLogService iNcTaskLogService;
    @Autowired
    IKDLService kdlService;
    @Autowired
    ScheduledMapper scheduledMapper;
    @Override
    public String getName() {
        QueryWrapper<Scheduled> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classpath",this.getClass().getName());
        Scheduled entity = scheduledMapper.selectOne(queryWrapper);
        return entity.getName();    }

    @Override
    public void execute() {
        //获取待上传的物料清单
        List<KDLMaterial> list= kdlService.getMaterialUploadList();
        logger.info(DateUtils.dateTimeNow() + "获取了" + list.size() + "个物料主数据");
        String billNo = "";
        NcTaskLog taskLog = new NcTaskLog();
        taskLog.setTaskType("物料主数据");
        taskLog.setBillCount(list.size());
        taskLog.setStartTime(DateUtils.now());
        if(list.size()>0) {
            for (int i = 0; i < list.size(); i++) {
                try
                {
                //String[] djhs = new String[1];
                //djhs[0] = list.get(i).getChukNo();

                //billNo+=list.size()>1?list.get(i).getChukNo()+",":list.get(i).getChukNo();
                JsonResult result = kdlService.submitMaterial(list.get(i).getId());
                //logger.info(DateUtils.dateTimeNow() + "提交了购进退出出库单：" + djhs[0]+",处理结果:"+result.getData().toString());
                logger.info(DateUtils.dateTimeNow() + "提交了物料：" + list.get(i).getMaterialcode());
                }catch(Exception ex){
                    logger.error(ex.getMessage());
                }
            }
        }
        taskLog.setBillNo(billNo);
        taskLog.setEndTime(DateUtils.now());
        iNcTaskLogService.save(taskLog);

    }
}
